﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using H2.Streams;

namespace Mystery.SCNR.Scripts
{
    public class ScriptingData
    {
        public const int Offset = 472;
        public const int ChunkSize = 128;

        public class PointSet
        {
            public const int Offset = 0;
            public const int ChunkSize = 48;

            public string Name;

            public class Point
            {
                public const int Offset = 32;
                public const int ChunkSize = 60;

                public string Name;

                public float PositionX;
                public float PositionY;
                public float PositionZ;

                public short ReferenceFrame;

                public int SurfaceIndex;

                public float FacingDirectionYaw;
                public float FacingDirectionPitch;

                public static Point[] Read(MapStream map, int pointSetOffset)
                {
                    Point[] points = new Point[map.ReadReflexiveAt(pointSetOffset + Offset)];

                    for (int i = 0; i < points.Length; i++)
                    {
                        Point p = new Point();
                        points[i] = p;

                        p.Name = map.ReadString(32, false);
                        
                        p.PositionX = map.ReadFloat();
                        p.PositionY = map.ReadFloat();
                        p.PositionZ = map.ReadFloat();

                        p.ReferenceFrame = map.ReadInt16();

                        map.Position += 2;

                        p.SurfaceIndex = map.ReadInt32();

                        p.FacingDirectionYaw = map.ReadFloat();
                        p.FacingDirectionPitch = map.ReadFloat();
                    }

                    return points;
                }
            }

            public Point[] Points;

            public short BSPIndex;
            public short ManualReferenceFrame;
            public int Flags;

            public static PointSet[] Read(MapStream map, int scriptingDataOffset)
            {
                PointSet[] pointSets = new PointSet[map.ReadReflexiveAt(scriptingDataOffset + Offset)];

                int startOffset = (int)map.Position;
                for (int i = 0; i < pointSets.Length; i++)
                {
                    int pointSetStartOffset = startOffset + (i * ChunkSize);

                    PointSet ps = new PointSet();
                    pointSets[i] = ps;

                    ps.Name = map.ReadString(32, false);

                    map.PushBookmark();
                    ps.Points = Point.Read(map, pointSetStartOffset);
                    map.PopBookmark();
                    map.Position += 8;

                    ps.BSPIndex = map.ReadInt16();
                    ps.ManualReferenceFrame = map.ReadInt16();
                    ps.Flags = map.ReadInt32();
                }

                return pointSets;
            }
        }

        public PointSet[] PointSets;

        public static ScriptingData Read(MapStream map, int scnrOffset)
        {
            map.ReadReflexiveAt(scnrOffset + Offset);
            ScriptingData sd = new ScriptingData();
            sd.PointSets = PointSet.Read(map, (int)map.Position);
            return sd;
        }
    }
}
